<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/4/19
 * Time: 10:31 AM
 */

namespace App\Models\CacheModel\zl5;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class Zl5ExplainType extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl5_explain_type';

    protected $cacheModel = 'explain.explain_type';

    /**
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function geTypes()
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setSort('DESC')->ZsGetIocData('list', function ()
        {
            try
            {
                $data = $this->where(['status' => 1])->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);
        return $this->batchGetInfoByIds(array_keys($ids));
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids], 'status' => 1];
        $field = 'title,extend,sort';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $id => $itemData)
        {
            $resData[$id] = arrayKeyData2int($itemData, ['id', 'sort']);

            if (isset($itemData['extend']))
            {
                $resData[$id]['extend'] = $itemData['extend'] ? json_decode($itemData['extend'], true) : [];
            }
        }

        return $resData;
    }
}
